AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize()   
self.Entity:SetModel( "models/props/cs_assault/ConsolePanelLoadingBay.mdl" ) 	
self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,  	
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   --after all, gmod is a physics  	
self.Entity:SetSolid( SOLID_VPHYSICS )        -- CHEESECAKE!    >:3            

 local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake() 
	end 
if StarGate.HasWire then
	self.Inputs = Wire_CreateInputs(self, { "Active" } )
	self.Outputs = Wire_CreateOutputs(self.Entity, {"Ready", "Charging", "Cooldown"})
end
self.Done = false
self.active = false
self.EndTime = nil
self.charging = false
self.cooldown = false
self.timeleft = StarGate.CFG:Get("EMP_Gen","countdown",10);
self.cooltime = StarGate.CFG:Get("EMP_Gen","Cooldowntime",60);
end   

function ENT:SpawnFunction( ply, tr)
	
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 60
	local ang = ply:GetAimVector():Angle(); ang.p = 0; ang.r = 0; ang.y = (ang.y+180) % 360
	
	local ent = ents.Create( "emp_gen" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetAngles(ang);
	
	return ent

end

 function ENT:Think()
 
	if self.charging and not self.cooldown then
		if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Ready", 0) end
		local area = ents.FindInSphere( self.Entity:GetPos(), 5500 )
		local chargestart = chargestart or false
		if not chargestart then
			for _,v in pairs(area) do
				if v:GetClass()== "player" then
					v:PrintMessage( HUD_PRINTCENTER, "Charging..." );
				end
			end
			chargestart = true
			self.Entity:NextThink( CurTime() + 1)
		end
		
		for _,v in pairs(area) do
			if v:GetClass() == "player" then
				v:PrintMessage( HUD_PRINTCENTER, ""..self.timeleft )
			end
		end
		if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Charging", self.timeleft) end
		self.timeleft = self.timeleft-1
		if self.timeleft != 0 then
			self.Entity:NextThink( CurTime() + 1 )
		end
		
		if self.timeleft < 0 then
			
			if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Charging", 0) end
			self.Entity:NextThink( CurTime() + .25)
			self.charging = false
			chargestart = false
			self.timeleft = StarGate.CFG:Get("EMP_Gen","countdown",10);
			self.active = true
			
		end
				
	else
		
		if not self.active  and not self.cooldown then
			if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Ready", 1) end
			self.Entity:NextThink( CurTime()+.25)
		end
		
	end
 
	if self.active then
	
		if not self.Done then
			
			self.List = ents.FindInSphere( self.Entity:GetPos(), 5500 )
			self.EndTime = CurTime() + 7
			self.Done = true
			local e = ents.Create("info_particle_system")
				e:SetPos(self.Entity:GetPos())
				e:SetAngles(Angle(0,0,0))
				e:SetKeyValue("effect_name","citadel_shockwave")
				e:SetKeyValue("start_active",1)
				e:Spawn()
				e:Activate()
				e:Fire("kill","",4)
			local e2 = ents.Create("info_particle_system")
				e2:SetPos(self.Entity:GetPos())
				e2:SetAngles(Angle(0,180,0))
				e2:SetKeyValue("effect_name","citadel_shockwave")
				e2:SetKeyValue("start_active",1)
				e2:Spawn()
				e2:Activate()
				e2:Fire("kill","",4)
			for _, v in pairs(self.List) do
				if v.Inputs or v.Outputs then
					local effectdata = EffectData() 
					effectdata:SetStart( v:GetPos() )
					effectdata:SetOrigin( v:GetPos() )
					effectdata:SetScale( 10 )
					effectdata:SetMagnitude( 10 )
					effectdata:SetEntity(v)
					util.Effect( "TeslaHitBoxes", effectdata ) --YAY lighning 
				end
			end	
		end
	
		for _,v in pairs(self.List) do
				self:EMP(v, true)
			end
	
		if CurTime() > self.EndTime then
			self.cooldown = true
		end
		
			self.Entity:NextThink( CurTime() + .25 )
	end
	
	if self.cooldown then
		if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Cooldown", self.cooltime) end
		if self.cooltime <=0 then
			self.cooldown = false
			self.cooltime = StarGate.CFG:Get("EMP_Gen","Cooldowntime",60);
			if StarGate.HasWire then Wire_TriggerOutput(self.Entity, "Ready", 0) end
			self.Entity:NextThink( CurTime() + .25 )
		else
			self.cooltime = self.cooltime - 1;
			self.Entity:NextThink( CurTime() + 1 )
		end
		if self.cooltime == StarGate.CFG:Get("EMP_Gen","Duration",30) then
			self.active = false
			self.Done = false
			for _,v in pairs(self.List) do
				self:EMP(v, false)
			end
		end
	end
	
	return true
end

function ENT:TriggerInput(iname, value)
	if ( value > 0 && iname == "Active") then
		if not self.cooldown then
			self.charging = true
		end
	end
end

function ENT:Use()
	if not self.cooldown then
		self.charging = true
	end
end

function ENT:EMP( entity , on ) --emp core Thank you AlgorithmX2
if (!entity or !entity:IsValid() ) then return end 
	if (entity) and (entity.Inputs) and type(entity.Inputs) == 'table' then
		for k,v in pairs(entity.Inputs) do
			if v.Type == "NORMAL" then
				if k == "Channel" then return end
				if (entity.TriggerInput) then
					if on then
						entity:TriggerInput( k , entity.Inputs[ k ].Value * 0 )
					else
						entity:TriggerInput( k , entity.Inputs[ k ].Value )
					end
				end
			elseif v.Type == "VECTOR" then
				if (entity.TriggerInput) then
					if on then
						entity:TriggerInput( k , entity.Inputs[ k ].Value * Vector(0,0,0) )
					else
						entity:TriggerInput( k , entity.Inputs[ k ].Value )
					end
				end
			end
		end
end
end
